package com.paw.ruoyi.admin.contants;

import java.util.HashMap;
import java.util.Map;

import cn.hutool.core.lang.Assert;

/**
 * @author Rubble
 * @date 2022/1/22
 **/
public class DataTypeMappingUtil {

  private static Map<String,String> dataTypeMap = new HashMap<>();

  private static Map<String,String> htmlTypeMap = new HashMap<>();

  static {
    dataTypeMap.put("char","String");
    dataTypeMap.put("varchar","String");
    dataTypeMap.put("nvarchar","String");
    dataTypeMap.put("varchar2","String");
    dataTypeMap.put("tinytext","String");
    dataTypeMap.put("mediumtext","String");
    dataTypeMap.put("blob","String");
    dataTypeMap.put("long varbinary","String");
    dataTypeMap.put("long varchar","String");
    dataTypeMap.put("long blob","String");
    dataTypeMap.put("long text","String");
    dataTypeMap.put("mediumblob","String");
    dataTypeMap.put("tinyblob","String");
    dataTypeMap.put("text","String");
    dataTypeMap.put("varbinary","String");
    dataTypeMap.put("json","String");

    dataTypeMap.put("datetime","Date");
    dataTypeMap.put("time","Date");
    dataTypeMap.put("date","Date");
    dataTypeMap.put("timestamp","Date");

    dataTypeMap.put("tinyint","Integer");
    dataTypeMap.put("tinyint unsigned","Integer");
    dataTypeMap.put("smallint","Integer");
    dataTypeMap.put("mediumint","Integer");
    dataTypeMap.put("mediumint unsigned","Integer");
    dataTypeMap.put("int","Integer");
    dataTypeMap.put("int unsigned","Integer");
    dataTypeMap.put("smallint unsigned","Integer");
    dataTypeMap.put("number","Integer");
    dataTypeMap.put("numeric","Integer");
    dataTypeMap.put("integer","Integer");
    dataTypeMap.put("integer unsigned","Integer");
    dataTypeMap.put("bit","Integer");
    dataTypeMap.put("binary","Integer");
    dataTypeMap.put("enum","Integer");
    dataTypeMap.put("year","Integer");

    dataTypeMap.put("bool","Boolean");

    dataTypeMap.put("bigint","Long");
    dataTypeMap.put("bigint unsigned","Long");
    dataTypeMap.put("float","Double");
    dataTypeMap.put("double","Double");
    dataTypeMap.put("double precision","Double");
    dataTypeMap.put("real","Double");

    dataTypeMap.put("decimal","BigDecimal");


    // javaType --> htmlType

    htmlTypeMap.put("String","input");
    htmlTypeMap.put("Integer","input");
    htmlTypeMap.put("Double","input");
    htmlTypeMap.put("BigDecimal","input");
    htmlTypeMap.put("Boolean","checkbox");
    htmlTypeMap.put("Date","datetime");

  }

  public static String getJavaType(String sqlType){
    Assert.notNull(sqlType);
    String type = dataTypeMap.get(sqlType.toLowerCase());
    if(type==null){
      type = "String";
    }
    return type;
  }


  public static String getHtmlType(String javaType){
    Assert.notNull(javaType);
    String type = htmlTypeMap.get(javaType);
    if(type==null){
      type = "input";
    }
    return type;
  }
}
